<?php

    namespace PHPK;

    require_once( dirname( __FILE__ ) . '/vendor/dompdf/dompdf_config.inc.php' );

    use DOMPDF;

    class File {

	/**
	 * Array containing File attributes
	 * @var array $_arFile
	 */
	private $_arFile = array();

	/**
	 * Magic method to initialize File
	 * @method __construct
	 * @param string $strFile name of PDF to constructed
	 * @param string $strCntnt HTML content to be saved as PDF
	 */
	public function __construct( $strFile, $strCntnt ) {

            $this->_arFile[ 'html' ] = '<!DOCTYPE html><html lang="en"><head>';
	    $this->_arFile[ 'html' ] .= '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
	    $this->_arFile[ 'html' ] .= '<title>PDFBldr</title><style>p{ text-align: justify }</style></head>';
	    $this->_arFile[ 'html' ] .= '<body>';
    	    $this->_arFile[ 'html' ] .= $strCntnt . '</body></html>';

	    $this->_arFile[ 'dpdf' ] = new DOMPDF();
	    $this->_arFile[ 'dpdf' ]->load_html( $this->_arFile[ 'html' ] );
	    $this->_arFile[ 'dpdf' ]->render();

	    $this->_arFile[ 'spdf' ] = $this->_arFile[ 'dpdf' ]->output();
	    $this->_arFile[ 'file' ] = fopen( $strFile, 'a+' );

	    fwrite( $this->_arFile[ 'file' ], $this->_arFile[ 'spdf' ] );
	    fclose( $this->_arFile[ 'file' ] );

	}

	/**
	 * Magic method to access File attributes
	 * @method __get
	 * @param string $strKey Key identifyng an attribute
	 * @return mixed Attribute of File identified by the key
	 */
        public function __get( $strKey ) {

	    if ( isset( $this->_arFile[ $strKey ] ) )
		return $this->_arFile[ $strKey ];

	    return false;

	}

    }

?>
